package com.paperobot.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.paperobot.common.util.PageUtil;
import com.paperobot.model.News;
import com.paperobot.service.PaperobotService;

public class NewsAction extends BaseAction{

	private static final long serialVersionUID = 1516834338392982332L;
	private PaperobotService paperobotService;;
	private News news;
	private List<News> newsList = new ArrayList<News>();
	private PageUtil pageUtil = new PageUtil();
	
	@Resource
	public void setPaperobotService(PaperobotService paperobotService) {
		this.paperobotService = paperobotService;
	}
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	public List<News> getNewsList() {
		return newsList;
	}
	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	
	@SuppressWarnings("unchecked")
	public String listNews(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			List<News> allNewsList = paperobotService.getNewsList();
			newsList = pageUtil.getCurrentPageRows(pagetype, allNewsList.size(), allNewsList);
			
			return SUCCESS;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String updateNews(){
		try {
			String newsId = request.getParameter("newsId");
			news = paperobotService.getNews(Long.parseLong(newsId));
			return "update";
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String deleteNews(){
		try {
			String newsId = request.getParameter("newsId");
			int rs = paperobotService.deleteNews(Long.parseLong(newsId));
			if(rs > 0){
				return "delete";
			}
			return ERROR;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String addNews(){

		 try {
			String title = request.getParameter("title");
			 String author = request.getParameter("author");
			 String source = request.getParameter("source");
			 String abstracts = request.getParameter("abstracts");
			 String content = request.getParameter("content");
			 String status = request.getParameter("status");
			      
			 News news = new News();
			 news.setTitle(title);
			 news.setAuthor(author);
			 news.setSource(source);
			 news.setAbstracts(abstracts);
			 news.setContent(content);
			 news.setStatus(Integer.parseInt(status));
			 news.setCreateTime(new Date());
			 
			 if(paperobotService.addNews(news) > 0){
			 	logger.info("咨询名称: 【"+ title + "】 添加成功。");
			 	request.setAttribute("result", "添加咨询成功！");
			 	return SUCCESS;
			 }else{
				logger.info("咨询名称: 【"+ title + "】 添加失败。");
			 	request.setAttribute("result", "添加咨询失败！");
			 	return ERROR;
			 }
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	
	}
	
	public String saveNews(){
		 try {
			String newsId = request.getParameter("newsId");
			 String title = request.getParameter("title");
			 String author = request.getParameter("author");
			 String source = request.getParameter("source");
			 String abstracts = request.getParameter("abstracts");
			 String content = request.getParameter("content");
			 String status = request.getParameter("status");
			 
			 News news = new News();
			 news.setNewsId(Long.parseLong(newsId));
			 news.setTitle(title);
			 news.setAuthor(author);
			 news.setSource(source);
			 news.setAbstracts(abstracts);
			 news.setContent(content);
			 news.setStatus(Integer.parseInt(status));
			 
			 if(paperobotService.updateNews(news) > 0){
			 	logger.info("咨询名称: "+ title + " 更新成功。");
			 	request.setAttribute("result", "更新咨询成功！");
			 	return SUCCESS;
			 }else{
				logger.info("咨询名称: "+ title + " 更新失败。");
			 	request.setAttribute("result", "更新咨询失败！");
			 	return ERROR;
			 }
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	
	
}



